/**
 * 
 */
package ro.dta.idbi.ui.patterns;

import java.awt.Point;
import java.util.Hashtable;
import java.util.Map;

import ro.dta.idbi.ui.sql.SqlGraph;

import com.mxgraph.model.mxCell;
import com.mxgraph.util.mxConstants;
import com.mxgraph.view.mxStylesheet;

/**
 * Graph for the normalisation pattern
 * 
 * @author Tudor Dobrila
 * 
 */
public class NormaliseGraph extends SqlGraph {

	private transient mxCell dropCell;
	private transient Point dropLocation;

	/**
	 * Normalisation graph class constructor
	 */
	public NormaliseGraph() {
		super();
		setDropEnabled(false);

		final mxStylesheet stylesheet = getStylesheet();
		this.extendStylesheet(stylesheet);
	}

	/**
	 * Get the drop location when moving a column
	 */
	public Point getDropLocation() {
		return dropLocation;
	}

	/**
	 * Get the drop cell when moving a column
	 */
	public mxCell getDropCell() {
		return dropCell;
	}

	@Override
	public Object[] moveCells(final Object[] cells, final double xDiff, final double yDiff,
			final boolean clone, final Object target, final Point location) {
		if (cells.length == 1) {
			dropCell = (mxCell) cells[0];
			dropLocation = location;
		}
		return super.moveCells(cells, xDiff, yDiff, clone, target, location);
	}

	@Override
	public boolean isValidDropTarget(final Object paramObject, final Object[] objects) {
		final mxCell dropTarget = (mxCell) paramObject;
		boolean result = false;

		if (dropTarget != null && dropTarget.getStyle() != null
				&& dropTarget.getStyle().equals("NEWCOLUMN")) {
			boolean isOk = true;
			for (Object crtObj : objects) {
				final mxCell crtCell = (mxCell) crtObj;

				if (!crtCell.getStyle().equals("COLUMN")
						&& !crtCell.getStyle().equals("PRIMARYKEY")) {
					isOk = false;
					break;
				}
			}

			if (isOk) {
				result = true;
			}
		}

		return result;
	}

	@Override
	public boolean isCellMovable(final Object arg0) {
		final mxCell cell = (mxCell) arg0;
		boolean result = false;

		if (cell.isVertex()) {
			result = true;
		}

		return result;
	}

	@Override
	public boolean isCellEditable(final Object paramObject) {
		final mxCell cell = (mxCell) paramObject;
		boolean result = false;

		if (cell.getStyle().equals("NEWTABLE")) {
			result = true;
		}

		return result;
	}

	/**
	 * Create the styles for the normalisation graph
	 */
	private void extendStylesheet(final mxStylesheet styleSheet) {
		// Style for tables
		final Map<String, Object> style = new Hashtable<String, Object>();
		style.put(mxConstants.STYLE_SHAPE, mxConstants.SHAPE_RECTANGLE);
		style.put(mxConstants.STYLE_VERTICAL_ALIGN, mxConstants.ALIGN_TOP);
		style.put(mxConstants.STYLE_FONTSTYLE, mxConstants.FONT_BOLD);
		style.put(mxConstants.STYLE_OPACITY, 50);
		style.put(mxConstants.STYLE_FONTCOLOR, "#111111");
		style.put(mxConstants.STYLE_FILLCOLOR, "#c9c9c9");
		style.put(mxConstants.STYLE_FONTSIZE, 13);
		styleSheet.putCellStyle("NEWTABLE", style);

		// Style for drop area for columns
		final Map<String, Object> style2 = new Hashtable<String, Object>();
		style2.put(mxConstants.STYLE_SHAPE, mxConstants.SHAPE_RECTANGLE);
		style2.put(mxConstants.STYLE_ALIGN, mxConstants.ALIGN_LEFT);
		style2.put(mxConstants.STYLE_FONTSIZE, 13);
		style2.put(mxConstants.STYLE_DASHED, "1");
		style2.put(mxConstants.STYLE_FILLCOLOR, "#dddddd");
		style2.put(mxConstants.STYLE_FONTCOLOR, "#000000");
		style2.put(mxConstants.STYLE_STROKEWIDTH, "1");
		style2.put(mxConstants.STYLE_STROKECOLOR, "#222222");
		styleSheet.putCellStyle("NEWCOLUMN", style2);

		// Style for determinant columns
		final Map<String, Object> style3 = new Hashtable<String, Object>();
		style3.put(mxConstants.STYLE_SHAPE, mxConstants.SHAPE_RECTANGLE);
		style3.put(mxConstants.STYLE_ALIGN, mxConstants.ALIGN_LEFT);
		style3.put(mxConstants.STYLE_FONTSIZE, 13);
		style3.put(mxConstants.STYLE_FILLCOLOR, "#CDB99C");
		style3.put(mxConstants.STYLE_FONTCOLOR, "#000000");
		style3.put(mxConstants.STYLE_FONTSTYLE, mxConstants.FONT_UNDERLINE);
		styleSheet.putCellStyle("DETERMINANT", style3);

		// Style for dependent columns
		final Map<String, Object> style4 = new Hashtable<String, Object>();
		style4.put(mxConstants.STYLE_SHAPE, mxConstants.SHAPE_RECTANGLE);
		style4.put(mxConstants.STYLE_ALIGN, mxConstants.ALIGN_LEFT);
		style4.put(mxConstants.STYLE_FONTSIZE, 13);
		style4.put(mxConstants.STYLE_FILLCOLOR, "#81A594");
		style4.put(mxConstants.STYLE_FONTCOLOR, "#000000");
		styleSheet.putCellStyle("DEPENDENT", style4);
	}
}
